/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import Entity.TacGia;
import Util.DatabaseHandle;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author copang
 */
public class TacGiaImpl implements TacGiaDAO{

    public boolean add(TacGia data) {
        String sql = "{ call sp_TACGIA_add(?,?,?,?,?,?,?) }";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        boolean flag;
        try {
            db.setOutParameter(1, java.sql.Types.INTEGER);
            db.setParameter(2, data.getTenTacGia());
            db.setParameter(3, data.isGioiTinh());
            db.setParameter(4, data.getDiaChiTG());
            db.setParameter(5, data.getTieuSu());
            db.setParameter(6, data.getDienThoaiTG());
            db.setParameter(7, data.getEmailTG());

            flag = db.executeStorePro();
            if (flag)
                data.setMaTacGia(db.getIntOutValue(1));
            
            db.closed();
            return flag;
        } catch (SQLException ex) {
            Logger.getLogger(TacGiaImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public boolean delete(int MaTacGia) {
        String sql = "{ call sp_TACGIA_delete(?) }";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        boolean flag;
        try {

            db.setParameter(1, MaTacGia);
            flag = db.executeStorePro();
            db.closed();
            return flag;
        } catch (SQLException ex) {
            Logger.getLogger(TacGiaImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public boolean edit(TacGia data) {
        String sql = "{ call sp_TACGIA_edit(?,?,?,?,?,?,?) }";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        boolean flag;
        try {
            db.setParameter(1, data.getMaTacGia());
            db.setParameter(2, data.getTenTacGia());
            db.setParameter(3, data.isGioiTinh());
            db.setParameter(4, data.getDiaChiTG());
            db.setParameter(5, data.getTieuSu());
            db.setParameter(6, data.getDienThoaiTG());
            db.setParameter(7, data.getEmailTG());

            flag = db.executeStorePro();
            db.closed();
            return flag;
        } catch (SQLException ex) {
            Logger.getLogger(TacGiaImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public ResultSet getAll() {
        
        String sql = "{call sp_TACGIA_getAll()}";
        DatabaseHandle db = new DatabaseHandle(sql, false);
        return db.excuteQuery();
    }

}
